<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.8.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1、引入zxing的依赖12345&amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;com.google.zxing&amp;lt;/groupId&amp;gt;    &amp;lt;artifactId&amp;gt;javase&amp;lt;/artifactId&amp;gt;    &amp;lt;version&amp;gt;3.4.0&amp;lt;/version&amp;gt;&amp;lt;/dependency&amp;gt; 2、二维码工具">
<meta property="og:type" content="article">
<meta property="og:title" content="使用zxing生成二维码">
<meta property="og:url" content="https://github.com/LBraveHeart/2021/03/24/码/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="1、引入zxing的依赖12345&amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;com.google.zxing&amp;lt;/groupId&amp;gt;    &amp;lt;artifactId&amp;gt;javase&amp;lt;/artifactId&amp;gt;    &amp;lt;version&amp;gt;3.4.0&amp;lt;/version&amp;gt;&amp;lt;/dependency&amp;gt; 2、二维码工具">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2021-03-24T02:57:19.009Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用zxing生成二维码">
<meta name="twitter:description" content="1、引入zxing的依赖12345&amp;lt;dependency&amp;gt;    &amp;lt;groupId&amp;gt;com.google.zxing&amp;lt;/groupId&amp;gt;    &amp;lt;artifactId&amp;gt;javase&amp;lt;/artifactId&amp;gt;    &amp;lt;version&amp;gt;3.4.0&amp;lt;/version&amp;gt;&amp;lt;/dependency&amp;gt; 2、二维码工具">

<link rel="canonical" href="https://github.com/LBraveHeart/2021/03/24/码/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>使用zxing生成二维码 | Hexo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2021/03/24/码/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          使用zxing生成二维码
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-03-24 10:53:28 / 修改时间：10:57:19" itemprop="dateCreated datePublished" datetime="2021-03-24T10:53:28+08:00">2021-03-24</time>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.2k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="1、引入zxing的依赖"><a href="#1、引入zxing的依赖" class="headerlink" title="1、引入zxing的依赖"></a>1、引入zxing的依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.google.zxing<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javase<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="2、二维码工具类的实现"><a href="#2、二维码工具类的实现" class="headerlink" title="2、二维码工具类的实现"></a>2、二维码工具类的实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.psk.hms.base.util;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.*;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.client.j2se.BufferedImageLuminanceSource;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.common.BitMatrix;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.common.HybridBinarizer;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;</span><br><span class="line"><span class="keyword">import</span> javax.imageio.ImageIO;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.geom.RoundRectangle2D;</span><br><span class="line"><span class="keyword">import</span> java.awt.image.BufferedImage;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.Hashtable;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二维码生成及读取工具类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> chenj.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2019/3/14 18:46</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">QRCodeUtil</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CHARSET = <span class="string">"UTF-8"</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String FORMAT_NAME = <span class="string">"JPG"</span>;</span><br><span class="line">    <span class="comment">// 二维码尺寸</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> QRCODE_SIZE = <span class="number">200</span>;</span><br><span class="line">    <span class="comment">// LOGO宽度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> WIDTH = <span class="number">60</span>;</span><br><span class="line">    <span class="comment">// LOGO高度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> HEIGHT = <span class="number">60</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建二维码图片</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回二维码图片</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> WriterException</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> IOException     BufferedImage</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BufferedImage <span class="title">createImage</span><span class="params">(String content, String logoImgPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> WriterException, IOException </span>&#123;</span><br><span class="line">        Hashtable&lt;EncodeHintType, Object&gt; hints = <span class="keyword">new</span> Hashtable&lt;EncodeHintType, Object&gt;();</span><br><span class="line">        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);</span><br><span class="line">        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);</span><br><span class="line">        hints.put(EncodeHintType.MARGIN, <span class="number">1</span>);</span><br><span class="line">        BitMatrix bitMatrix = <span class="keyword">new</span> MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);</span><br><span class="line">        <span class="keyword">int</span> width = bitMatrix.getWidth();</span><br><span class="line">        <span class="keyword">int</span> height = bitMatrix.getHeight();</span><br><span class="line">        BufferedImage image = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>; x &lt; width; x++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> y = <span class="number">0</span>; y &lt; height; y++) &#123;</span><br><span class="line">                image.setRGB(x, y, bitMatrix.get(x, y) ? <span class="number">0xFF000000</span> : <span class="number">0xFFFFFFFF</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (logoImgPath == <span class="keyword">null</span> || <span class="string">""</span>.equals(logoImgPath)) &#123;</span><br><span class="line">            <span class="keyword">return</span> image;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 插入图片</span></span><br><span class="line">        QRCodeUtil.insertImage(image, logoImgPath, needCompress);</span><br><span class="line">        <span class="keyword">return</span> image;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加Logo</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> source       二维码图片</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> IOException void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">insertImage</span><span class="params">(BufferedImage source, String logoImgPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        File file = <span class="keyword">new</span> File(logoImgPath);</span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Image src = ImageIO.read(<span class="keyword">new</span> File(logoImgPath));</span><br><span class="line">        <span class="keyword">int</span> width = src.getWidth(<span class="keyword">null</span>);</span><br><span class="line">        <span class="keyword">int</span> height = src.getHeight(<span class="keyword">null</span>);</span><br><span class="line">        <span class="keyword">if</span> (needCompress) &#123; <span class="comment">// 压缩LOGO</span></span><br><span class="line">            <span class="keyword">if</span> (width &gt; WIDTH) &#123;</span><br><span class="line">                width = WIDTH;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (height &gt; HEIGHT) &#123;</span><br><span class="line">                height = HEIGHT;</span><br><span class="line">            &#125;</span><br><span class="line">            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);</span><br><span class="line">            BufferedImage tag = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</span><br><span class="line">            Graphics g = tag.getGraphics();</span><br><span class="line">            g.drawImage(image, <span class="number">0</span>, <span class="number">0</span>, <span class="keyword">null</span>); <span class="comment">// 绘制缩小后的图</span></span><br><span class="line">            g.dispose();</span><br><span class="line">            src = image;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 插入LOGO</span></span><br><span class="line">        Graphics2D graph = source.createGraphics();</span><br><span class="line">        <span class="keyword">int</span> x = (QRCODE_SIZE - width) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> y = (QRCODE_SIZE - height) / <span class="number">2</span>;</span><br><span class="line">        graph.drawImage(src, x, y, width, height, <span class="keyword">null</span>);</span><br><span class="line">        Shape shape = <span class="keyword">new</span> RoundRectangle2D.Float(x, y, width, width, <span class="number">6</span>, <span class="number">6</span>);</span><br><span class="line">        graph.setStroke(<span class="keyword">new</span> BasicStroke(<span class="number">3f</span>));</span><br><span class="line">        graph.draw(shape);</span><br><span class="line">        graph.dispose();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带Logo的二维码</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destPath     二维码输出路径</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String logoImgPath, String destPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);</span><br><span class="line">        mkdirs(destPath);</span><br><span class="line">        ImageIO.write(image, FORMAT_NAME, <span class="keyword">new</span> File(destPath));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成不带Logo的二维码</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content  二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destPath 二维码输出路径</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String destPath)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        QRCodeUtil.encode(content, <span class="keyword">null</span>, destPath, <span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带Logo的二维码，并输出到指定的输出流</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> output       输出流</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String logoImgPath, OutputStream output, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);</span><br><span class="line">        ImageIO.write(image, FORMAT_NAME, output);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成不带Logo的二维码，并输出到指定的输出流</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content 二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> output  输出流</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, OutputStream output)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        QRCodeUtil.encode(content, <span class="keyword">null</span>, output, <span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 二维码解析</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> file 二维码</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回解析得到的二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception String</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">decode</span><span class="params">(File file)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image;</span><br><span class="line">        image = ImageIO.read(file);</span><br><span class="line">        <span class="keyword">if</span> (image == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        BufferedImageLuminanceSource source = <span class="keyword">new</span> BufferedImageLuminanceSource(image);</span><br><span class="line">        BinaryBitmap bitmap = <span class="keyword">new</span> BinaryBitmap(<span class="keyword">new</span> HybridBinarizer(source));</span><br><span class="line">        Result result;</span><br><span class="line">        Hashtable&lt;DecodeHintType, Object&gt; hints = <span class="keyword">new</span> Hashtable&lt;DecodeHintType, Object&gt;();</span><br><span class="line">        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);</span><br><span class="line">        result = <span class="keyword">new</span> MultiFormatReader().decode(bitmap, hints);</span><br><span class="line">        String resultStr = result.getText();</span><br><span class="line">        <span class="keyword">return</span> resultStr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 二维码解析</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> path 二维码存储位置</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回解析得到的二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception String</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">decode</span><span class="params">(String path)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> QRCodeUtil.decode(<span class="keyword">new</span> File(path));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断路径是否存在，如果不存在则创建</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dir 目录</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">mkdirs</span><span class="params">(String dir)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (dir != <span class="keyword">null</span> &amp;&amp; !<span class="string">""</span>.equals(dir)) &#123;</span><br><span class="line">            File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">            <span class="keyword">if</span> (!file.isDirectory()) &#123;</span><br><span class="line">                file.mkdirs();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3、测试"><a href="#3、测试" class="headerlink" title="3、测试"></a>3、测试</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">生成不带logo的二维码</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String dir = <span class="string">"d:/qrcode.jpg"</span>;</span><br><span class="line">    String content = <span class="string">"www.baidu.com"</span>;</span><br><span class="line">    File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">    QRCodeUtil.encode(content, <span class="keyword">new</span> FileOutputStream(file));</span><br><span class="line">    System.out.println(QRCodeUtil.decode(file));</span><br><span class="line">&#125;</span><br><span class="line">生成带logo的二维码</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String dir = <span class="string">"d:/qrcode.jpg"</span>;</span><br><span class="line">    String content = <span class="string">"www.baidu.com"</span>;</span><br><span class="line">    String logoImgPath = <span class="string">"d:/logo.png"</span>;</span><br><span class="line">    File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">    QRCodeUtil.encode(content, logoImgPath, <span class="keyword">new</span> FileOutputStream(file), <span class="keyword">true</span>);</span><br><span class="line">    System.out.println(QRCodeUtil.decode(file));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/09/10/va阻塞队列/" rel="prev" title="java阻塞队列">
      <i class="fa fa-chevron-left"></i> java阻塞队列
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、引入zxing的依赖"><span class="nav-number">1.</span> <span class="nav-text">1、引入zxing的依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、二维码工具类的实现"><span class="nav-number">2.</span> <span class="nav-text">2、二维码工具类的实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、测试"><span class="nav-number">3.</span> <span class="nav-text">3、测试</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">liu666</p>
  <div class="site-description" itemprop="description">仰面朝天，知此身渺渺，沧海一粟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">55</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/LBraveHeart" title="GitHub → https://github.com/LBraveHeart"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:446109730@qq.com" title="E-Mail → mailto:446109730@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liu666</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">58k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">53 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>












  

  

</body>
</html>
